package com.oamicnet.boot.validation.controller;

import com.oamicnet.boot.validation.controller.dto.UserDTO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户管理")
@RestController
@RequestMapping("/user")
public class UserController {

    @Operation(summary = "根据ID获取用户")
    // GET 请求，路径参数
    @GetMapping("/{id}")
    public String getUser(@Parameter(description = "用户ID", example = "1011")  @PathVariable("id") Long id) {
        return "获取用户，ID=" + id;
    }

    @Operation(summary = "根据名称获取用户")
    // GET 请求，查询参数
    @GetMapping
    public String queryUser(@Parameter(description = "用户名", required = true, example = "John")
                                @RequestParam String name) {
        return "查询用户，name=" + name;
    }

    @Operation(summary = "新增用户")
    // POST 请求，接收 JSON 参数并校验
    @PostMapping
    public String createUser(@Valid @RequestBody UserDTO user) {
        return "创建用户：" + user.getName();
    }

    @Operation(summary = "更新用户")
    // PUT 请求，接收 JSON 参数并校验
    @PutMapping("/{id}")
    public String updateUser(@PathVariable Long id, @Valid @RequestBody UserDTO user) {
        return "更新用户：" + id + "，新用户名：" + user.getName();
    }

    @Operation(summary = "删除用户")
    // DELETE 请求，路径参数
    @DeleteMapping("/{id}")
    public String deleteUser(@Parameter(description = "用户ID", example = "1011") @PathVariable Long id) {
        return "删除用户，ID=" + id;
    }
}
